/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bo;

import data.Departments;
import dao.DepartmentDAO;
import TableView.DepartmentTable;
import javax.swing.ComboBoxModel;
import data.Deptinterdependency;
import java.sql.SQLException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import data.Deptinterdependency;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import org.hibernate.tool.stat.AbstractTreeModel;
import view.DepartmentsDependenciesManager;

/**
 *
 * @author ChiHieu
 */
public class DepartmentsBO {

	private DepartmentDAO dDAO;
	Boolean error = false;

	public DepartmentsBO(DepartmentDAO dDAO) {
		this.dDAO = dDAO;
	}

	public DepartmentsBO() {
		dDAO = new DepartmentDAO();
	}
	String infomation = "";

	public String getInfomation() {
		return infomation;
	}

	public void setInfomation(String infomation) {
		this.infomation = infomation;
	}

	public List<Departments> ShowAlldepartment() {

		List<Departments> ldept = new dao.DepartmentDAO().getAlldepartment();

		return ldept;
	}

	public Boolean actionAdd(String depname, String head, String location) {
		//BO
		for (Departments dm : dDAO.getAll()) {
			if (depname.equals(dm.getDeptname())) {
				error = true;
				infomation += "DEPNAME already exist!\n";
				break;
			}
		}
		if (depname.length() == 0) {
			error = true;
			infomation += "You forgot to fill in the DEPNAME!\n";
		}
		if (head.length() == 0) {
			error = true;
			infomation += "You forgot to fill in the DEPHEAD!\n";

		}
		if (location.length() == 0) {
			error = true;
			infomation += "You forgot to fill in the LOCATION!\n";

		}
		//DAO

		if (!error) {// nếu có thì action=>BO
			Departments dp = new Departments();
			dp.setDeptname(depname);
			dp.setDepthead(head);
			dp.setLocation(location);
			try {
				if (dDAO.executeInsertHQLQuery(dp)) {//
					infomation = "Insert your data has been successfully!";
				} else {
					infomation = "can not connect to database!";
				}
			} catch (Exception ex) {
				infomation = "sao đéo đc" + ex.getMessage();
			}
		}
		return error;
	}

	public boolean actionUpdate(Departments dm) {
		if (dm.getDeptname().toString().length() == 0) {
			error = true;
			infomation += "You forgot to fill in the DEPNAME!\n";
		}
		if (dm.getDepthead().toString().length() == 0) {
			error = true;
			infomation += "You forgot to fill in the DEPHEAD!\n";
		}
		if (dm.getLocation().toString().length() == 0) {
			error = true;
			infomation += "You forgot to fill in the LOCATION!\n";
		}
		if (!error) {
			dDAO.executeUpdateHQLQuery(dm);
			infomation = "Edit your data has been successfully!";
		}
		return error;
		//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
	}

	public boolean actionDelete(Departments departments) {
		if (dDAO.executeDeleteHQLQuery(departments)) {
			infomation = "Deleteting has been successfully!";
			return true;
		} else {
			infomation = "Could not Delete !";
			return false;
		}
	}

	public void actionSearch(String text, String text0, String text1, String text2) {
		String sql = "";
	}

	public DepartmentTable actionShowTable() {
		return dDAO.ShowTableDepartment();
	}

	public ComboBoxModel getComboBoxModel() {
		return dDAO.getComboBoxModel();
	}

	public boolean actionCreateDEPTINTERDEPENDENCY(String dept, String upper, String lower) {
		dDAO = new DepartmentDAO();
		error = false;
		infomation = null;
		String uptmp;
		String lowertmp;
		if ("-----None-----".equals(upper)) {
			uptmp = "0";
		} else {
			uptmp = dDAO.getDepartmentByName(upper).getDeptno() + "";
		}
		if ("-----None-----".equals(lower)) {
			lowertmp = "0";
		} else {
			lowertmp = dDAO.getDepartmentByName(lower).getDeptno() + "";
		}
		if ("-----None-----".equals(dept)) {// || "-----None-----||".equals(upper) || "-----None-----".equals(lower)) {
			infomation = "Please check the your information!";
		} else {
			Deptinterdependency dp = new Deptinterdependency();
			dp.setDepartments(dDAO.getDepartmentByTagName(dept));// không get đc chỗ này nè má ơi
			dp.setUpdeptno(Integer.parseInt(uptmp));
			dp.setDndeptno(Integer.parseInt(lowertmp));

			if (dDAO.executeInsertHQLQuery(dp)) {
				infomation = "Insert your data has been successfully!";
			} else {
				infomation = "Please check the your information!";
			}

		}
		return error;
	}

	public boolean actionUpdateDEPTINTERDEPENDENCY(int idtmp, String dept, String upper, String lower) {
		dDAO = new DepartmentDAO();
		error = false;
		infomation = null;
		String uptmp;
		String lowertmp;
		if ("-----None-----".equals(upper)) {
			uptmp = "0";
		} else {
			uptmp = dDAO.getDepartmentByName(upper).getDeptno() + "";
		}
		if ("-----None-----".equals(lower)) {
			lowertmp = "0";
		} else {
			lowertmp = dDAO.getDepartmentByName(lower).getDeptno() + "";
		}
		if ("-----None-----".equals(dept)) {// || "-----None-----||".equals(upper) || "-----None-----".equals(lower)) {
			infomation = "Please check the your information!";
			error = true;
		} else {
			Deptinterdependency dp = new Deptinterdependency();
			//update database dependency
			dp.setId(idtmp);
			dp.setDepartments(dDAO.getDepartmentByTagName(dept));
			dp.setUpdeptno(Integer.parseInt(uptmp));
			dp.setDndeptno(Integer.parseInt(lowertmp));
			if (dDAO.executeUpdateHQLQuery(dp)) {
				infomation = "Insert your data has been successfully!";
			} else {
				infomation = "Please check the your information!";
			}
		}
		return error;
	}

	public TableModel actionShowTableDepinterDependency() {
		return dDAO.ShowTableTableDepinterDependency();
	}

	public void actionDeleteDepDependency(String id) {
		Deptinterdependency dptmp = new Deptinterdependency();
		if (dDAO.executeDeleteHQLQuery(id)) {
			setInfomation("Delete Object has been successfully!");
		} else {
			setInfomation("Delete Object faile");
		}
	}

	public Departments findDeptbyDeptNo(Integer deptno) {
		Departments tmpDept = new DepartmentDAO().getDepartmentById(deptno);
		return tmpDept;

	}

	public void actionDeleteDepDependency(Deptinterdependency dptmp) {
		if (dDAO.executeDeleteHQLQuery(dptmp)) {
			setInfomation("Delete Object has been successfully!");
		} else {
			setInfomation("Delete Object faile");
		}
	}

	public TreeNode actionTest() {
		DefaultMutableTreeNode model = new DefaultMutableTreeNode();
		for (Deptinterdependency dp : dDAO.getALLDepinterdependency()) {
			if (dp.getUpdeptno() == 0) {
				model.add(new DefaultMutableTreeNode(dp.getDepartments().getDeptname()));
			}
		}
		return model;
	}
	public void getListMutable() {// run init
		for (Deptinterdependency dp : dDAO.getALLDepinterdependency()) {
				lsMutable.add(new DefaultMutableTreeNode(dp.getDepartments().getDeptname()));
		}
	}
	List<DefaultMutableTreeNode> lsMutable = new ArrayList<>();
	public List<DefaultMutableTreeNode> getChildList(String depname) {
		List<DefaultMutableTreeNode> list = new ArrayList<>();
		int deptno = dDAO.getDepartmentByName(depname).getDeptno();
		
		for (Deptinterdependency dp : dDAO.getALLDepinterdependency()) {
			//model1.getUserObject().toString()
			if (dp.getUpdeptno() == deptno) {
				DefaultMutableTreeNode tmp = new DefaultMutableTreeNode(dp.getDepartments().getDeptname());
				list.add(tmp);
					}
		}
		return list;

	}
}
